package app.modules.cloudpivot.organization;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.engine.api.model.system.RelatedCorpSettingModel;
import com.authine.cloudpivot.engine.service.organization.exception.DataSourceException;
import com.authine.cloudpivot.engine.spi.dto.OrgConfigMetaData;
import com.authine.cloudpivot.engine.spi.dto.OrgConfigParams;
import com.authine.cloudpivot.engine.spi.service.OrgSyncAdapterRuntimeFactory;
import com.authine.cloudpivot.engine.spi.service.OrgSyncAdapterRuntimeService;

/**
 * 自定义组织同步配置
 *
 * @author LiuGangQiang Create in 2024/04/12
 */
@Component
public class CustomOrgRuntimeFactory implements OrgSyncAdapterRuntimeFactory {
    private static final Logger LOG = LoggerFactory.getLogger(CustomOrgRuntimeFactory.class);

    @Override
    public OrgConfigMetaData getConfig() {
        List<OrgConfigParams> params = new ArrayList<>();
        params.add(OrgConfigParams.builder().code("require_param").description("必填参数").required(true).build());
        params.add(OrgConfigParams.builder().code("optional_param").description("可选参数").required(false).build());
        return OrgConfigMetaData.builder().adapterCode("custom").adapterName("二开示例").configList(params).build();

    }

    @Override
    public OrgSyncAdapterRuntimeService getRuntimeService(String corpId, Map<String, String> config) {
        return new CustomOrgRuntimeServiceImpl(corpId, config);
    }

    @Override
    public void registerCallBackUrl(RelatedCorpSettingModel setting) throws DataSourceException {
        /* FIXME 这里用来主动调接口往第三方注册注册增量回调地址 类似专有钉 */
        LOG.info("===>>这里执行增量注册逻辑 组织名称：{} CorpId：{}", setting.getName(), setting.getCorpId());
        JSONObject syncConfig = JSONObject.parseObject(setting.getSyncConfig());
        syncConfig.forEach((k, v) -> {
            LOG.info("===>>参数名：{} 参数值：{}", k, v);
        });

    }
}